package cn.xej.health.user.controller;

import cn.xej.health.common.constant.AuthConstant;
import cn.xej.health.common.domain.UserDto;
import cn.xej.health.common.utils.*;
import cn.xej.health.user.entity.User;
import cn.xej.health.user.feign.AuthFeignService;
import cn.xej.health.user.feign.TeamFeignService;
import cn.xej.health.user.service.UserService;
import cn.xej.health.user.vo.VoUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author xej
 * 用户管理
 */
@RefreshScope
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TeamFeignService teamFeignService;



    @GetMapping("/list")
    public CommonResult list(@RequestParam("pageIndex")Integer pageIndex,@RequestParam("pageSize")Integer pageSize,@RequestParam("searchName")String searchName){
        return userService.queryList(pageIndex,pageSize,searchName);
    }

    @GetMapping("/info/{username}")
    public CommonResult info(@PathVariable("username")String username){
        return userService.queryInfo(username);
    }

    @PostMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] uuids){
        List<Integer> uuidList = Arrays.asList(uuids);
        boolean flag = userService.removeByIds(uuidList);
        return flag ? CommonResult.success(null) : CommonResult.failed();
    }

    @GetMapping("/check/{username}")
    public CommonResult check(@PathVariable("username")String username){
        User user = userService.getOne(new QueryWrapper<User>().eq("username", username));
        if(user == null){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    @PutMapping("/update")
    public CommonResult update(@RequestBody User user){
        userService.updateById(user);
        return CommonResult.success(null);
    }

    @PutMapping("/updateStatus/{uuid}")
    public CommonResult updateStatus(@PathVariable("uuid")Integer uuid,@RequestParam Integer status){
        userService.update(null,new UpdateWrapper<User>().eq("uuid",uuid).set("status",status));
        return CommonResult.success(null);
    }

    @PutMapping("/resetPwd/{uuid}")
    public CommonResult resetPwd(@PathVariable("uuid")Integer uuid){
        userService.update(null,new UpdateWrapper<User>().eq("uuid",uuid).set("password", Util.getPwd(Constant.PASSWORD)));
        return CommonResult.success(null);
    }

    @GetMapping("/getUser/{uuid}")
    public CommonResult getUser(@PathVariable("uuid")Integer uuid){
        User user = userService.getById(uuid);
        return CommonResult.success(user);
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody User user){
        user.setCreateDate(new Date());
        user.setPassword(Util.getPwd(user.getPassword()));
        user.setRid(2);
        userService.save(user);
        return CommonResult.success(null);
    }

    @GetMapping("/team/{id}")
    public CommonResult team(@PathVariable("id")Integer id){
        return teamFeignService.info(id);
    }


    @GetMapping("/captainList")
    public CommonResult captainList(){
        return userService.queryCaptainList();
    }


    /**
     * 根据用户名获取通用用户信息
     * @param username
     * @return
     */
    @GetMapping("/loadUserByUsername")
    public UserDto loadUserByUsername(@RequestParam String username){
        UserDto userDTO = userService.loadUserByUsername(username);
        return userDTO;
    }


    /**
     * 登录
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody VoUser voUser) {
        return userService.login(voUser.getUsername(),voUser.getPassword());
    }

    /**
     * 获取该团队的医生
     * @param tid
     * @return
     */
    @PostMapping("/initMemberList/{tid}")
    public CommonResult initMemberList(@PathVariable("tid")Integer tid,@RequestParam("pageIndex")Integer pageIndex,@RequestParam("pageSize")Integer pageSize,@RequestParam("searchName")String searchName){
        return userService.initMemberList(tid,pageIndex,pageSize,searchName);
    }

    /**
     * 获取剩余的医生
     * @return
     */
    @GetMapping("/initRemainUserList")
    public CommonResult initRemainUserList(){
        return userService.initRemainUserList();
    }


    @GetMapping("/getTeamDockerList")
    public CommonResult getTeamDockerList(@RequestParam("uuidList")String uuidList){
        return userService.getTeamDockerList(uuidList);
    }
}
